package com.sunnay.util;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sunnay.vo.common.Page;
import com.sunnay.vo.live.RoomVo;
import com.sunnay.vo.product.ProductDetailVo;
import com.sunnay.vo.product.ProductItemsVo;
import com.sunnay.vo.product.ProductVo;
import com.sunnay.vo.product.SaleVo;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@UtilityClass
public class WptUtils {
    /**
     * 直播间详细信息
     * @param nickName
     * @param shopUri
     * @return
     */
    public static RoomVo roomDetailInfo(String nickName, String shopUri){
        try{
            System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
            Authenticator.setDefault(new Authenticator() {
                                         public PasswordAuthentication getPasswordAuthentication() {
                                             //serverProxyUsername 用户名
                                             //serverProxyPassword 密码
                                             return new PasswordAuthentication(CommUtils.proxyUser, CommUtils.proxyPass.toCharArray());
                                         }
                                     }
            );
            Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(CommUtils.proxyServer,CommUtils.proxyPort));
            String result = HttpUtil.createGet(CommUtils.SEARCH_SHOP + shopUri)
                    .contentType("application/json")
                    .setProxy(proxy)
                    .setConnectionTimeout(8000)
                    .setReadTimeout(8000)
                    .execute().body();

            if(StringUtils.isNotBlank(result)){
                com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(result);
                String code = jsonObject.getString("code");
                RoomVo roomVo = new RoomVo();
                if(StringUtils.equals(code,"0")){
                    String roomName = jsonObject.getJSONObject("data").getString("roomName");
                    if (nickName.equals(roomName)){
                        roomVo.setRoomId(jsonObject.getJSONObject("data").getString("id"));
                        roomVo.setTimId(jsonObject.getJSONObject("data").getString("timId"));
                        return roomVo;
                    }
                }
            }
        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }
        return null;
    }
    /**
     * 获取拍品最新价格
     * @param saleUri
     * @return
     */
    public ProductDetailVo productDetailInfo(String saleUri){
        try{
            System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
            Authenticator.setDefault(new Authenticator() {
                     public PasswordAuthentication getPasswordAuthentication() {
                         //serverProxyUsername 用户名
                         //serverProxyPassword 密码
                         return new PasswordAuthentication(CommUtils.proxyUser, CommUtils.proxyPass.toCharArray());
                     }
                 }
            );
            Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(CommUtils.proxyServer,CommUtils.proxyPort));
            String body = HttpUtil.createGet(String.format(CommUtils.PRODUCT_DETAIL, saleUri))
                    .contentType("application/json")
                    .setProxy(proxy)
                    .setConnectionTimeout(8000)
                    .setReadTimeout(8000)
                    .execute().body();
            if(StringUtils.isNotBlank(body)){
                JSONObject jsonObject = JSONUtil.parseObj(body);
                ProductDetailVo detailVo = new ProductDetailVo();
                detailVo.setNowTime(jsonObject.getLong("nowTime"));
                detailVo.setShopName(jsonObject.getJSONObject("data").getJSONObject("shop").getStr("nickname"));
                detailVo.setIncrease(jsonObject.getJSONObject("data").getJSONObject("sale").getJSONObject("priceJson").getInt("increase"));
                SaleVo saleVo = JSONUtil.toBean(jsonObject.getJSONObject("data").getStr("sale"), SaleVo.class);
                detailVo.setUri(saleVo.getUri());
                detailVo.setNowPrice(0);
                detailVo.setEndTime(saleVo.getEndTime());
                if(!saleVo.getBid().getBidList().isEmpty() && saleVo.getBid().getBidList().size() > 0){
                    detailVo.setNowPrice(saleVo.getBid().getBidList().get(0).getPrice());
                    detailVo.setNickName(saleVo.getBid().getBidList().get(0).getNickname());
                }
                return detailVo;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 获取店铺拍品
     * @param shopUri
     * @param page
     * @return
     */
    public List<ProductVo> productList(String shopUri, Page page){
        try{

            System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
            Authenticator.setDefault(new Authenticator() {
                 public PasswordAuthentication getPasswordAuthentication() {
                     //serverProxyUsername 用户名
                     //serverProxyPassword 密码
                     return new PasswordAuthentication(CommUtils.proxyUser, CommUtils.proxyPass.toCharArray());
                 }
              }
            );
            Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(CommUtils.proxyServer,CommUtils.proxyPort));
            String body = HttpUtil.createGet(String.format(CommUtils.SHOP_PRODUCT_LIST, shopUri, page.getPageNum(),page.getSort()))
                    .contentType("application/json")
                    .setProxy(proxy)
                    .setConnectionTimeout(8000)
                    .setReadTimeout(8000)
                    .execute().body();
            System.out.println("拍品列表："+body);
            if(StringUtils.isNotBlank(body)){
                List<ProductVo> list = new ArrayList<>();
                JSONObject jsonObject = JSONUtil.parseObj(body);
                ProductItemsVo productItemsVo = JSONUtil.toBean(jsonObject.getJSONObject("data"), ProductItemsVo.class);
                Optional.ofNullable(productItemsVo.getItems()).ifPresent(items -> {
                    items.forEach(data->{
                        ProductVo productVo = new ProductVo();
                        BeanUtils.copyProperties(data, productVo);
                        list.add(productVo);
                    });
                });
                return list;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /*public static void main(String[] args) {
        String body = HttpUtil.createGet(String.format(CommUtils.SHOP_PRODUCT_LIST, "2101301031VApuNj", ""))
                .contentType("application/json")
                .setConnectionTimeout(8000)
                .setReadTimeout(8000)
                .execute().body();
        System.out.println(body);
        JSONObject jsonObject = JSONUtil.parseObj(body);
        ProductItemsVo productItemsVo = JSONUtil.toBean(jsonObject.getJSONObject("data"), ProductItemsVo.class);
        Optional.ofNullable(productItemsVo.getItems()).ifPresent(items -> {
            items.forEach(data->{
                ProductVo productVo = new ProductVo();
                BeanUtils.copyProperties(data, productVo);
                System.out.println(productVo);
            });
        });
    }*/
}
